﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using Traceability.API.Applications.Command.PictureCommand;
using Traceability.Domain;
using Traceability.Domain.DTO.Output;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.PictureCommandHandler
{
    public class PictureQueryCommandHandler : IRequestHandler<PictureQueryCommand, APIResult<List<PictureOutput>>>
    {
        private readonly IBaseRepository<Picture> pictureRepository;

        public PictureQueryCommandHandler(IBaseRepository<Picture> pictureRepository)
        {
            this.pictureRepository = pictureRepository;
        }

        public Task<APIResult<List<PictureOutput>>> Handle(PictureQueryCommand request, CancellationToken cancellationToken)
        {
          var res= pictureRepository.GetAll().AsNoTracking().Where(s=>!s.IsDeltede&&s.Batch==request.Batch&&s.SourceType==request.SourceType&&s.StageType==request.StageType).Select(s=> new PictureOutput{
                 Id=s.Id,
                 Batch=s.Batch,
                 PictureURL=s.PictureURL,
            });


            APIResult < List < PictureOutput >> aPIResult = new APIResult<List<PictureOutput>>()
            {
                Code = EnumCode.Ok,
                Msg = "显示成功",
                Data = res.ToList()
            };

            return Task.FromResult(aPIResult);

        }
    }
}
